package com.sky.utils;

import java.io.File;
import java.lang.reflect.Field;

public class Utils {
    public static String generateFiles(String path) throws Exception {
        String dirPath = path + "\\test";
        File dir = new File(dirPath);
        if (!dir.exists()) {
            // 创建多级目录
            dir.mkdirs();
            System.out.println("创建" + dirPath + "文件夹成功");
        } else {
            System.out.println("创建" + dirPath + "文件夹失败");
        }

        String filePath = dirPath + "\\hello.txt";
        File file = new File(filePath);
        if (!file.exists()) {
            // 创建指定文件
            file.createNewFile();
            System.out.println("创建" + filePath + "文件成功");
            return filePath;
        } else {
            System.out.println("创建" + filePath + " 文件失败");
            return null;
        }
    }

    public static String getSuffix(String name) throws Exception {
        int i = name.lastIndexOf(".");
        String substring = name.substring(i);
        return substring;
    }

    public static String camelToUnderscore(String camel) {
        // 使用正则表达式将小驼峰格式的字符串替换为下划线格式
        return camel.replaceAll("([A-Z])", "_$1").toLowerCase();
    }

    /**
     * 实体类中id必须在首位,否则update会有问题
     * id必须自增
     *
     * @param aClass    类.getclass()
     * @param tableName 表名
     * @param id        实体类中id字段
     */
    public static void getSql(Class aClass, String tableName, String id) {
        Field[] fields = aClass.getDeclaredFields();
        System.out.println("--------------------update-------------------");
        System.out.println("update " + tableName);
        System.out.println("<set>");
        // 遍历属性并输出属性信息
        for (int i = 0; i < fields.length; i++) {
            // 获取属性名
            String fieldName = fields[i].getName();
            // 获取属性类型
            Class<?> fieldType = fields[i].getType();
            // 输出属性信息
            String s = Utils.camelToUnderscore(fieldName);
            if (fieldName == id) {
                continue;
            }
            System.out.print("\t<if test=\"");
            System.out.print(fieldName + " != null\">");
            if (i != 1) {
                System.out.print(",");
            }
            System.out.print(s);
            System.out.print(" = #{" + fieldName + "}</if>");
            System.out.println();
        }
        System.out.println("</set>");
        System.out.println("where id = #{id}");
        System.out.println("--------------------deleteByIds-------------------");
        System.out.println("delete from " + tableName);
        System.out.println("where " + id + " in");
        System.out.println("<foreach collection=\"ids\" item=\"id\" separator=\",\" open=\"(\" close=\")\">");
        System.out.println("\t#{id}");
        System.out.println("</foreach>");

        System.out.println("--------------------insert-------------------");
        System.out.println("insert into " + tableName);
        System.out.print("(");
        for (int i = 0; i < fields.length; i++) {
            // 获取属性名
            String fieldName = fields[i].getName();
            // 输出属性信息
            String s = Utils.camelToUnderscore(fieldName);
            if (fieldName == id) {
                continue;
            }
            System.out.print(s);
            if (i != fields.length - 1) {
                System.out.print(",");
            }
        }
        System.out.print(") VALUES");
        System.out.println();
        System.out.print("(");
        for (int i = 0; i < fields.length; i++) {
            // 获取属性名
            String fieldName = fields[i].getName();
            // 输出属性信息
            if (fieldName == id) {
                continue;
            }
            System.out.print("#{" + fieldName + "}");
            if (i != fields.length - 1) {
                System.out.print(",");
            }
        }
        System.out.print(")");
        System.out.println();
        System.out.println("--------------------selectByQuery-------------------");
        System.out.println("select * from " + tableName);
        System.out.println("<where>");
        for (int i = 0; i < fields.length; i++) {
            // 获取属性名
            String fieldName = fields[i].getName();
            String s = Utils.camelToUnderscore(fieldName);
            System.out.print("\t<if test=\"");
            System.out.print(fieldName + " != null\">");
            System.out.print("and " + s + " = #{" + fieldName + "}</if>");
            System.out.println();
        }
        System.out.println("</where>");
    }

    public static void getController(Class clazz) {
        System.out.println("import org.springframework.beans.factory.annotation.Autowired;");
        System.out.println("import org.springframework.web.bind.annotation.*;");
        System.out.println();
        System.out.println("import java.util.List;");
        System.out.println();
        System.out.println("@RestController");
        System.out.println("@RequestMapping(\"/" + clazz.getSimpleName().toLowerCase() + "\")");
        System.out.println("public class " + clazz.getSimpleName() + "Controller {");
        System.out.println("\t@Autowired");
        System.out.println("\tprivate " + clazz.getSimpleName() + "Service " + clazz.getSimpleName().toLowerCase() + "Service;");
        System.out.println("\t@GetMapping(\"/findAll\")");
        System.out.println("\tpublic List<" + clazz.getSimpleName() + "> findAll(){");
        System.out.println("\t\treturn " + clazz.getSimpleName().toLowerCase() + "Service.findAll();");
        System.out.println("\t}");
        System.out.println("\t@GetMapping(\"/findById/{id}\")");
        System.out.println("\tpublic " + clazz.getSimpleName() + " findById(@PathVariable(\"id\") Integer id){");
        System.out.println("\t\treturn " + clazz.getSimpleName().toLowerCase() + "Service.findById(id);");
        System.out.println("\t}");
        System.out.println("\t@PostMapping(\"/save\")");
        System.out.println("\tpublic void save(@RequestBody " + clazz.getSimpleName() + " " + clazz.getSimpleName().toLowerCase() + "){");
        System.out.println("\t\t" + clazz.getSimpleName().toLowerCase() + "Service.save(" + clazz.getSimpleName().toLowerCase() + ");");
        System.out.println("\t}");
        System.out.println("\t@PutMapping(\"/update\")");
        System.out.println("\tpublic void update(@RequestBody " + clazz.getSimpleName() + " " + clazz.getSimpleName().toLowerCase() + "){");
        System.out.println("\t\t" + clazz.getSimpleName().toLowerCase() + "Service.update(" + clazz.getSimpleName().toLowerCase() + ");");
        System.out.println("\t}");
        System.out.println("\t@DeleteMapping(\"/deleteById/{id}\")");
        System.out.println("\tpublic void deleteById(@PathVariable(\"id\") Integer id){");
        System.out.println("\t\t" + clazz.getSimpleName().toLowerCase() + "Service.deleteById(id);");
        System.out.println("\t}");
        System.out.println("}");
        System.out.println("--------------------service-------------------");
        System.out.println("import java.util.List;");
        System.out.println("public interface " + clazz.getSimpleName() + "Service {");
        System.out.println("\tList<" + clazz.getSimpleName() + "> findAll();");
        System.out.println("\t" + clazz.getSimpleName() + " findById(Integer id);");
        System.out.println("\tvoid save(" + clazz.getSimpleName() + " " + clazz.getSimpleName().toLowerCase() + ");");
        System.out.println("\tvoid update(" + clazz.getSimpleName() + " " + clazz.getSimpleName().toLowerCase() + ");");
        System.out.println("\tvoid deleteById(Integer id);");
        System.out.println("}");
        System.out.println("--------------------serviceImpl-------------------");
        System.out.println("import org.springframework.beans.factory.annotation.Autowired;");
        System.out.println("import org.springframework.stereotype.Service;");
        System.out.println("import org.springframework.transaction.annotation.Transactional;");
        System.out.println("import java.util.List;");
        System.out.println("@Service");
        System.out.println("@Transactional");
        System.out.println("public class " + clazz.getSimpleName() + "ServiceImpl implements " + clazz.getSimpleName() + "Service {");
        System.out.println("\t@Autowired");
        System.out.println("\tprivate " + clazz.getSimpleName() + "Mapper " + clazz.getSimpleName().toLowerCase() + "Mapper;");
        System.out.println("\t@Override");
        System.out.println("\tpublic List<" + clazz.getSimpleName() + "> findAll() {");
        System.out.println("\t\treturn " + clazz.getSimpleName().toLowerCase() + "Mapper.findAll();");
        System.out.println("\t}");
        System.out.println("\t@Override");
        System.out.println("\tpublic " + clazz.getSimpleName() + " findById(Integer id) {");
        System.out.println("\t\treturn " + clazz.getSimpleName().toLowerCase() + "Mapper.findById(id);");
        System.out.println("\t}");
        System.out.println("\t@Override");
        System.out.println("\tpublic void save(" + clazz.getSimpleName() + " " + clazz.getSimpleName().toLowerCase() + ") {");
        System.out.println("\t\t" + clazz.getSimpleName().toLowerCase() + "Mapper.save(" + clazz.getSimpleName().toLowerCase() + ");");
        System.out.println("\t}");
        System.out.println("\t@Override");
        System.out.println("\tpublic void update(" + clazz.getSimpleName() + " " + clazz.getSimpleName().toLowerCase() + ") {");
        System.out.println("\t\t" + clazz.getSimpleName().toLowerCase() + "Mapper.update(" + clazz.getSimpleName().toLowerCase() + ");");
        System.out.println("\t}");
        System.out.println("\t@Override");
        System.out.println("\tpublic void deleteById(Integer id) {");
        System.out.println("\t\t" + clazz.getSimpleName().toLowerCase() + "Mapper.deleteById(id);");
        System.out.println("\t}");
        System.out.println("}");
        System.out.println("--------------------mapper-------------------");
        System.out.println("import org.apache.ibatis.annotations.*;");
        System.out.println("import java.util.List;");
        System.out.println("@Mapper");
        System.out.println("public interface " + clazz.getSimpleName() + "Mapper {");
        System.out.println("\t@Select(\"select * from " + Utils.camelToUnderscore(clazz.getSimpleName()) + "\")");
        System.out.println("\tList<" + clazz.getSimpleName() + "> findAll();");
        System.out.println("\t@Select(\"select * from " + Utils.camelToUnderscore(clazz.getSimpleName()) + " where id = #{id}\")");
        System.out.println("\t" + clazz.getSimpleName() + " findById(Integer id);");
        System.out.println("\t@Insert(\"insert into " + Utils.camelToUnderscore(clazz.getSimpleName()) + " values(null,#{name},#{age})\")");
        System.out.println("\tvoid save(" + clazz.getSimpleName() + " " + clazz.getSimpleName().toLowerCase() + ");");
        System.out.println("\t@Update(\"update " + Utils.camelToUnderscore(clazz.getSimpleName()) + " set name = #{name},age = #{age} where id = #{id}\")");
        System.out.println("\tvoid update(" + clazz.getSimpleName() + " " + clazz.getSimpleName().toLowerCase() + ");");
        System.out.println("\t@Delete(\"delete from " + Utils.camelToUnderscore(clazz.getSimpleName()) + " where id = #{id}\")");
        System.out.println("\tvoid deleteById(Integer id);");
        System.out.println("}");
    }

}
